package cc.eddic.pratice.programmingparadigms.emp01array.impl;

import cc.eddic.pratice.programmingparadigms.Employee;
import cc.eddic.pratice.programmingparadigms.Result;
import cc.eddic.pratice.programmingparadigms.emp01array.EmployeeStatisticsService;
import cc.eddic.pratice.programmingparadigms.emp02list.ResultList;
import cc.eddic.pratice.programmingparadigms.emp02list.impl.ResultListCxyImpl;


public class StatisticsCxyImpl extends ReaderGcsImpl implements EmployeeStatisticsService {

    /**
     *按照性别统计
     * @param employees
     * @return 统计结果 数组
     */
    //TODO:未实现性别统计
//    public Result[] pivotByGender(Employee[] employees){
//        Result [] gender = new Result[employees.length];
//
//        int male = 0 ;
//        for (int i=0; i<employees.length-1; i++){
//            if(employees[i].getGender().equals("男")){
//                male++;
//            }
//            return new Result[]{
//                    new Result("男", male),
//                    new Result("女",employees.length - male)
//            };
//        }
//        throw new UnsupportedOperationException();
//    }
    public Result[] pivotByGender(Employee[] employees){
        int index = 0;
        Result[] results = new Result[employees.length];
        for (Employee employee : employees){
            boolean isFound = false;
            for (Result result : results){
                if (null == result){
                    continue;
                }
                if (result.getKey().equals(employee.getGender())){
                    isFound = true;
                    result.setCount(result.getCount()+1);
                    break;
                }
            }
            if (!isFound){
                results[index++] = new Result(employee.getGender(), 1);
            }
        }
        Result[] newResults = new Result[index];
        for (int i=0; i<index; i++){
            newResults[i] = results[i];
        }
        return newResults;
    }


    /**
     * 按照部门分类
     * @param employees Employee 数组
     * @return 统计结果数组
     */
    //TODO:未实现部门统计
    public Result[] pivotByDepartment(Employee[] employees){
        int index = 0;
        Result[] results = new Result[employees.length];
        for (Employee employee : employees){
            boolean isFound = false;
            for (Result result : results){
                if (null == result){
                    continue;
                }
                if (result.getKey().equals(employee.getDepartment())){
                    isFound = true;
                    result.setCount(result.getCount()+1);
                    break;
                }
            }
            if (!isFound){
                results[index++] = new Result(employee.getDepartment(), 1);
            }
        }
        Result[] newResults = new Result[index];
        for (int i=0; i<index; i++){
            newResults[i] = results[i];
        }
        return newResults;

//        throw new UnsupportedOperationException();
    }

    /**
     * 按照职位分类
     * @param
     * @return 统计结果数组
     */
    //TODO:未实现职位统计
    public Result[] pivotByJobTitle(Employee[] employees){
//        throw  new UnsupportedOperationException();

        int index = 0;
        Result[] results = new Result[employees.length];
        for (Employee employee : employees){
            boolean isFound = false;
            for (Result result : results){
                if (null == result){
                    continue;
                }
                if (result.getKey().equals(employee.getJobTitle())){
                    isFound = true;
                    result.setCount(result.getCount()+1);
                    break;
                }
            }
            if (!isFound){
                results[index++] = new Result(employee.getJobTitle(), 1);
            }
        }
        Result[] newResults = new Result[index];
        for (int i=0; i<index; i++){
            newResults[i] = results[i];
        }
        bubbleSort(newResults);
        return newResults;
//        return results;
    }

    public Result[] bubbleSort(Result[] results){
        for (int i=results.length-1;i>0;i--){
            for(int j=results.length-1;j>results.length-1-i;j--){
                if (results[j].getCount()>results[j-1].getCount()){
                    Result exchange = results[j-1];
                    results[j-1] = results[j];
                    results[j] = exchange;
                }
            }
        }
        return results;
    }



}
